package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.entity.NewsEntity;
import com.dz.Incubator.service.NewsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/api/news")
@Api(tags = {"新闻资讯"})
@Validated
public class NewsController {

    private static final Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Autowired
    private NewsService newsService;

    /**
     * 集合查找 NewsEntity 对象
     *
     * @param ids 集合查找数据
     */
    @PostMapping(value = "/find")
    @ApiOperation("根据ID批量查找")
    public InvokeResult<List<NewsEntity>> find(@RequestBody Long[] ids) {
        InvokeResult invokeResult = null;
        try {
            if (null == ids && ids.length <= 0) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.find(ids));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 分页查询 news 对象
     *
     * @param pagination 条件
     */
    @PostMapping(value = "/findByPageList")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "新闻分类")
    })
    public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
        InvokeResult invokeResult = null;
        try {
            if (null == pagination) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
            invokeResult = InvokeResult.success(newsService.findByPagination(pagination));
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }


    /**
     * 查找所有对象
     */
    @GetMapping(value = "/findAll")
    @ApiOperation("查询所有")
    public InvokeResult<List<NewsEntity>> findAll() {
        InvokeResult invokeResult = null;
        try {
            invokeResult = InvokeResult.success(newsService.findAll());
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 新增数据
     *
     * @param newsEntity 新增对象
     */
    @PostMapping(value = "/insert")
    @ApiOperation("新增数据")
    public InvokeResult<NewsEntity> insert(@Valid @RequestBody NewsEntity newsEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null == newsEntity) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.insert(newsEntity));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 修改数据
     *
     * @param newsEntity 修改对象
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改数据")
    public InvokeResult<NewsEntity> update(@Valid @RequestBody NewsEntity newsEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null == newsEntity) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.update(newsEntity));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 获取单个NewsEntity 对象
     *
     * @param id Id
     */
    @GetMapping(value = "get/{id}")
    @ApiOperation("根据Id查询")
    public InvokeResult<NewsEntity> get(@PathVariable Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null == id) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.get(id));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 删除单个NewsEntity 对象
     *
     * @param id 删除数据 ID
     */
    @GetMapping(value = "/delete/{id}")
    @ApiOperation("根据Id删除")
    public InvokeResult delete(@PathVariable Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null == id) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.delete(id));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }


    /**
     * 批量添加数据
     *
     * @param list 添加对象列表
     */
    @PostMapping(value = "/insertBatch")
    @ApiOperation("批量添加")
    public InvokeResult<List<NewsEntity>> insertBatch(@Valid @RequestBody List<NewsEntity> list) {
        InvokeResult invokeResult = null;
        try {
            if (null == list || list.size() <= 0) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.insertBatch(list));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 批量修改数据
     *
     * @param list 修改对象列表
     */
    @PostMapping(value = "/updateBatch")
    @ApiOperation("批量修改")
    public InvokeResult<List<NewsEntity>> updateBatch(@Valid @RequestBody List<NewsEntity> list) {
        InvokeResult invokeResult = null;
        try {
            if (null == list || list.size() <= 0) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.updateBatch(list));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 批量删除数据
     *
     * @param ids 删除对象ids数组
     */
    @PostMapping(value = "/delBatch")
    @ApiOperation("批量删除")
    public InvokeResult<List<NewsEntity>> delBatch(@RequestBody Long[] ids) {
        InvokeResult invokeResult = null;
        try {
            if (null == ids || ids.length <= 0) {
                return invokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            } else {
                invokeResult = InvokeResult.success(newsService.deleteBatch(ids));
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            logger.error(e.getMessage(), e);
        }
        return invokeResult;
    }

}